package com.zoe.onelink.enhancer.monitor;

import com.zoe.onelink.util.ApplicationUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.ReadinessState;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-01-24
 */
public class ApplicationInfoPrinter implements ApplicationListener<AvailabilityChangeEvent<ReadinessState>> {

    @Value("${onelink.monitor.printServerInfo:true}")
    private boolean printServerInfo;

    @Override
    public void onApplicationEvent(AvailabilityChangeEvent<ReadinessState> availabilityChangeEvent) {
        // ReadinessState.ACCEPTING_TRAFFIC 表示可以接受流量
        if (availabilityChangeEvent.getState() == ReadinessState.ACCEPTING_TRAFFIC && this.printServerInfo) {
            try {
                ApplicationUtil.printServerInfo((ConfigurableApplicationContext) availabilityChangeEvent.getSource(),
                        true, false, true);
            } catch (Exception e) {
                // ignore
            }
        }
    }

}
